package com.sj.parksdk;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;

import androidx.core.app.ActivityCompat;

import com.luck.picture.lib.tools.PictureFileUtils;
import com.sj.parksdk.setting.Dictionary;
import com.sj.parksdk.utils.sdkinit.ANRWatchDogInit;
import com.sj.parksdk.utils.sdkinit.XBasicLibInit;
import com.xuexiang.xui.XUI;

public class ParkSdk {
    public static String deviceId;
    public static ParkSdk sInstance;
    public static Context sContext;

    public ParkSdk() {

    }

    public static Context getContext() {
        return sContext;
    }

    /**
     * 获取单例
     *
     * @return
     */
    public static ParkSdk getInstance() {
        if (sInstance == null) {
            synchronized (ParkSdk.class) {
                if (sInstance == null) {
                    sInstance = new ParkSdk();
                }
            }
        }
        return sInstance;
    }

    /**
     * 初始化
     *
     * @param application
     */
    public static void init(Application application) {
        sContext = application;
        //解决4.x运行崩溃的问题
        initLibs(application);
    }

    /**
     * 初始化基础库
     */
    private static void initLibs(Application application) {
        //基础库初始化
        XBasicLibInit.init(application);
        //XUI初始化
        XUI.init(application);
        XUI.debug(ParkSdk.isDebug());
        PictureFileUtils.setAppName("park");
        //运营统计数据运行时不初始化
        if (!ParkSdk.isDebug()) {
//            UMengInit.init(application);
        }
        //ANR监控
        ANRWatchDogInit.init();
//        CameraView.setICameraStrategy(new AutoCameraStrategy(1920 * 1080));
        Dictionary.getInstance().init();

        deviceId = getIMEI();
    }

    public static String getDeviceId() {
        return deviceId;
    }

    public static boolean isDebug() {
        return true;
    }

    /**
     * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return 手机IMEI
     */
    public static String getIMEI() {
        TelephonyManager tm = (TelephonyManager) sContext.getSystemService(Activity.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(sContext, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return "000000000";
        }
        return tm.getDeviceId();
    }

}
